using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Main.controls;

namespace Main.polls
{
    public partial class show1 : System.Web.UI.Page
    {
        private Poll _currentPoll;
        public Poll currentPoll
        {
            get { return _currentPoll; }
            set { _currentPoll = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //First, we need to load a poll into our system.
            _currentPoll = new Poll();
            if (Request.QueryString["id"] == null)
            {
                //If the query string isn't present
                //redirect to RolyPoll home
                Response.Redirect("/default.aspx");
            }
            else
            {
                _currentPoll.FillByID(Convert.ToInt32(Request.QueryString["id"]));
                if (_currentPoll.Question == null || _currentPoll.Question.Equals(""))
                {
                    //So, if this page isn't a valid poll ID, we'll
                    //redirect to a 404 page.
                    Response.Clear();
                    Response.StatusCode = 404;
                    Response.End();
                    Server.Transfer("");
                }
            }
            //If you are still with us, we now have a poll in the page
            //That's good, right?
            //So, at this point, we need to fill the page with
            //some useful data
            pollTitle.Text = currentPoll.Title;
            pollQuestion.Text = currentPoll.Question;
            pollSummary.Text = currentPoll.Summary;
            //Next, we need to render some options:
            foreach (Option option in currentPoll.Options)
            {
                pollOptionList.Items.Add(new ListItem(option.PollOption, option.OptionID.ToString()));
            }
            pollVote.PostBackUrl = "/handlers/vote.ashx?id=" + currentPoll.ID +"&key=" + pollOptionList.UniqueID;
            
            //Next, let's add the comments to the page.
            CommentCollection comms = new CommentCollection();
            comms.GetCommentsForPoll(currentPoll.ID);
            ShowComment comment;
            foreach (Comment co in comms)
            {
                comment = (ShowComment)LoadControl("/controls/ShowComment.ascx");
                comment.CurrentComment = co;
                comment.CommentID = co.ID;
                pollComments.Controls.Add(comment);
            }
            newcommentform.PollID = currentPoll.ID;
            //Finally, if they are logged in, let's show them the Add Comment Form
            //lets test sometime
            currentResults.CurrentPoll = currentPoll;
        }
    }
}
